package com.work.straighttalk.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.hyphenate.chat.EMMessage;
import com.work.straighttalk.bean.StraightTalkMessage;
import com.work.straighttalk.model.UserModel;

/**
 * Created by dllo on 16/3/4.
 */
public class ChatRVAdapter extends BaseRVAdapter<StraightTalkMessage> {
    private OnVoicePlayerListener voicePlayerListener;

    /**
     * 特有的 接口 用于控制播放音频
     *
     * @param voicePlayerListener
     */
    public void setVoicePlayerListener(OnVoicePlayerListener voicePlayerListener) {
        this.voicePlayerListener = voicePlayerListener;
    }

    @Override
    public BaseViewHolder<StraightTalkMessage> onCreateViewHolder(ViewGroup parent, int viewType) {
        //context 只加载一次
        Context context = parent.getContext();
        switch (viewType) {
            case 10:
                return new ChatSendViewHolder(context, parent, getListener());
            case 20:
                return new ReceiveTextHolder(context, parent, getListener());
            case 11:
                return new SendVoiceHolder(context, parent, voicePlayerListener);
            case 12:
                return new ChatImageHolder(context, parent);
            case 22:
                return new ReceiveImageHolder(context, parent);
            case 21:
                return new SendVoiceHolder(context, parent, voicePlayerListener);
            default:
                return new ChatSendViewHolder(context, parent, getListener());

        }
    }

    @Override
    public int getItemViewType(int position) {
        //TODO 此部分代码有些长 需要重新考虑
        if (getLists().get(position).getEmMessage().direct() == EMMessage.Direct.SEND) {
            switch (getLists().get(position).getEmMessage().getType()) {
                case TXT:
                    return 10;
                case VOICE:
                    return 11;
                case IMAGE:
                    return 12;
                case VIDEO:
                    return 13;
                case FILE:
                    return 14;
                case LOCATION:
                    return 15;
                case CMD:
                    return 16;
            }
        } else {
            switch (getLists().get(position).getEmMessage().getType()) {
                case TXT:
                    return 20;
                case VOICE:
                    return 21;
                case IMAGE:
                    return 22;
                case VIDEO:
                    return 23;
                case FILE:
                    return 24;
                case LOCATION:
                    return 25;
                case CMD:
                    return 26;
                default:
                    return 0;
            }
        }
        return 0;
    }

    public int getItemByID(String msgID) {
        for (int i = 0; i < getItemCount(); i++) {
            if (getLists().get(i).getEmMessage().getMsgId().equals(msgID)) {
                return i;
            }
        }
        return -1;
    }

}
